package com.kaishengit.web;


import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.entity.AtMessage;
import com.kaishengit.entity.Message;
import com.kaishengit.entity.Task;
import com.kaishengit.entity.User;
import com.kaishengit.service.CommentService;
import com.kaishengit.service.MainService;
import com.kaishengit.service.MessageService;
import com.kaishengit.util.Page;

public class MainServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;
	private MessageService mservice=new MessageService();
	private CommentService cservice=new CommentService();
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String q=request.getParameter("qmethod");
		if(q==null||"".equals(q)){
			toProject(request,response);
		}else if("new".equals(q)){
			toPost(request,response);
		}else if("comment".equals(q)){
			toCom(request,response);
		}
		
	}
	
	
	/**
	 * 提交"你在想些什么"里面的内容
	 * @throws IOException 
	 * */

	private void toPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HttpSession session= request.getSession();
		String projectid=(String) session.getAttribute("projectid");
		User user=(User) session.getAttribute("user");
		String content=request.getParameter("content");
		String type="infor";
		mservice.save(projectid,user.getId(),content,type);
		response.sendRedirect("pro.jspx");
		
	}
	
	/**
	 * 提交对"你在想些什么"的评论
	 * @throws IOException 
	 * */
	private void toCom(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String messageid=request.getParameter("mid");
		String con=request.getParameter("desc");
		HttpSession session= request.getSession();
		User user=(User) session.getAttribute("user");
		String userid=user.getId();
		cservice.save(messageid, con, userid);
		response.sendRedirect("pro.jspx");
	}

	

	
	
	private void toProject(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		/**
		 * 显示该用户的任务
		 * */
		MainService service=new MainService();
		HttpSession session= request.getSession();
		String projectid=(String) session.getAttribute("projectid");
		User user=(User) session.getAttribute("user");
		session.setAttribute("projectid", projectid);
		List<Task> list=service.findByuserIdAndProjectId(user.getId(),projectid);
		
		/**
		 * 显示所有信息流
		 * */
		String q = request.getParameter("q");
		int pageNum =0;
		if(q == null || "".equals(q)){
			pageNum = 1;
		}else{
			pageNum = Integer.valueOf(q);
		}
		Page<Message> messagelist=mservice.findAllByProjectId(pageNum ,projectid);
		request.setAttribute("messagelist", messagelist);
		 //显示关于我的信息流
		String mepageNum = request.getParameter("p");
		Page<AtMessage> meList=mservice.findByUserId(user.getId(),mepageNum);
		request.setAttribute("meList",meList );
		request.setAttribute("taskList", list);
		request.getRequestDispatcher("WEB-INF/views/project.jsp").forward(request, response);
		
		
		
	}
	
	
	

}
